/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.clarice.homechef.utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Akshay Velhal
 */
@Repository 
public class Utility {
    
    @Autowired
    DataSource dataSource;
    
    
    private static String sqlGetMealNames = "select name from meal";
    public List<String> getMealNames() throws Exception {
        List<String> mealNames = new ArrayList<>();
        try (Connection conn = dataSource.getConnection();
                PreparedStatement ps = conn.prepareStatement(sqlGetMealNames);) {
            ResultSet rs = ps.executeQuery();
            while(rs.next()) {
                mealNames.add(rs.getString("name"));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            throw ex;
        }
        return mealNames;
    }
    
    private static String sqlIsValidUser = "select name from users where email = ?";
    public String isValidUser(String email) throws Exception {
        String isValid = "false";
        try (Connection conn = dataSource.getConnection();
                PreparedStatement ps = conn.prepareStatement(sqlIsValidUser);) {
            
            ps.setString(1, email);
            ResultSet rs = ps.executeQuery();
            while(rs.next()) {
                isValid = rs.getString("name");
            }
        }catch (Exception ex) {
            ex.printStackTrace();
            throw ex;
        }
        return isValid;
    }
    
    private static String sqlIsValidChef = "select name from chef where email = ?";
    public String isValidChef(String email) throws Exception {
        String isValid = "false";
        try (Connection conn = dataSource.getConnection();
                PreparedStatement ps = conn.prepareStatement(sqlIsValidChef);) {
            
            ps.setString(1, email);
            ResultSet rs = ps.executeQuery();
            while(rs.next()) {
                isValid = rs.getString("name");
            }
        }catch (Exception ex) {
            ex.printStackTrace();
            throw ex;
        }
        return isValid;
    }
    
}
